﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using SlamCms.SharePoint.Core;
using System.Linq;
using Slam.FieldTypes;
using SlamCms.SharePoint.FieldTypes;

namespace SlamCms.SharePoint.UtilityPages
{
	public partial class UpgradeNoteFields : LayoutsPageBase
	{
		protected override void OnInit(EventArgs e)
		{
			btnUpdate.Click += new EventHandler(btnUpdate_Click);
            btnUpdateLists.Click += new EventHandler(btnUpdateLists_Click);
            btnUpgradeToNote.Click += new EventHandler(btnUpgradeToNote_Click);
			base.OnInit(e);
		}

		protected void Page_Load(object sender, EventArgs e)
		{	
		}

        void btnUpgradeToNote_Click(object sender, EventArgs e)
        {
            {
                string[] fieldsToUpdate = this.txtFields.Text.Split(';');

                try
                {
                    SlamCmsContext.SharePoint.Monitor("Upgrade CK to Note ").SiteCollection(sc =>
                    {
                        for (int k = 0; k < sc.AllWebs.Count; k++)
                        {
                            SPWeb web = sc.AllWebs[k];
                            for (int i = 0; i < web.Fields.Count; i++)
                            {
                                SPField field = web.Fields[i];
                                if (field.TypeAsString.Equals("Slam Field", StringComparison.OrdinalIgnoreCase) && fieldsToUpdate.Contains(field.Title))
                                {
                                    field.SchemaXml = field.SchemaXml.Replace("Slam Field", "Note");
                                    web.AllowUnsafeUpdates = true;
                                    field.Update(true);
                                }
                            }

                            for (int j = 0; j < web.Lists.Count; j++)
                            {
                                SPList list = web.Lists[j];

                                for (int i = 0; i < list.Fields.Count; i++)
                                {
                                    SPField field = list.Fields[i];
                                    if (field.TypeAsString.Equals("Slam Field", StringComparison.OrdinalIgnoreCase) && fieldsToUpdate.Contains(field.Title))
                                    {
                                        field.SchemaXml = field.SchemaXml.Replace("Slam Field", "Note");
                                        web.AllowUnsafeUpdates = true;
                                        field.Update(true);
                                    }
                                }
                            }
                            web.Dispose();
                        }
                    });
                }
                catch (Exception ex)
                {
                    messages.Text = ex.ToString();
                }
            }
        }

		void btnUpdate_Click(object sender, EventArgs e)
		{
            string[] fieldsToUpdate = this.txtFields.Text.Split(';');

			try
			{
				SlamCmsContext.SharePoint.Monitor("Upgrade Note Fields").SiteCollection(sc =>
				{
					for (int k = 0; k < sc.AllWebs.Count; k++)
					{
						SPWeb web = sc.AllWebs[k];
						for (int i = 0; i < web.Fields.Count; i++)
						{
							SPField field = web.Fields[i];

                            if(field.Type.Equals(SPFieldType.Note) && fieldsToUpdate.Contains(field.Title))
							{
								field.SchemaXml = field.SchemaXml.Replace("Note", "Slam Field");
								web.AllowUnsafeUpdates = true;
								field.Update(true);
                                // Change the field to be a CKEditorField
                                AssignCKEditorField(web.Fields[i]);
							}

                            

						}

						for (int j = 0; j < web.Lists.Count; j++)
						{
							SPList list = web.Lists[j];

							for (int i = 0; i < list.Fields.Count; i++)
							{
								SPField field = list.Fields[i];
                                if (field.Type.Equals(SPFieldType.Note) && fieldsToUpdate.Contains(field.Title))
								{
                                    field.SchemaXml = field.SchemaXml.Replace("Note", "Slam Field");
									web.AllowUnsafeUpdates = true;
									field.Update(true);
                                    // CHange the field to be a CKEditorField
                                    AssignCKEditorField(list.Fields[i]);
								}
							}
						}
						web.Dispose();
					}
				});
			}
			catch (Exception ex)
			{
				messages.Text = ex.ToString();
			}
		}

        void btnUpdateLists_Click(object sender, EventArgs e)
        {
            string[] fieldsToUpdate = this.txtFields.Text.Split(';');

            try
            {
                SlamCmsContext.SharePoint.Monitor("Upgrade Slam Field Fields").SiteCollection(sc =>
                {
                    for (int k = 0; k < sc.AllWebs.Count; k++)
                    {
                        SPWeb web = sc.AllWebs[k];

                        for (int j = 0; j < web.Lists.Count; j++)
                        {
                            SPList list = web.Lists[j];

                            for (int i = 0; i < list.Fields.Count; i++)
                            {
                                SPField field = list.Fields[i];
                                if (field.TypeAsString.Equals("Slam Field", StringComparison.OrdinalIgnoreCase) && fieldsToUpdate.Contains(field.Title))
                                {
                                    web.AllowUnsafeUpdates = true;
                                    field.Update(true);
                                    // CHange the field to be a CKEditorField
                                    AssignCKEditorField(list.Fields[i]);
                                }
                            }
                        }
                        web.Dispose();
                    }
                });
            }
            catch (Exception ex)
            {
                messages.Text = ex.ToString();
            }
        }

        private void AssignCKEditorField(SPField field)
        {
            SlamField slamField = field as SlamField;

            if (slamField != null)
            {
                slamField.UserControl = typeof(CKEditorFieldControl).AssemblyQualifiedName;
                slamField.Update(true);
            }
        
        }
	}
}
